﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;
using MvcApplication2.Models.Services;
using System.IO;

namespace MvcApplication2.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new HomeModel();
            model.LoaiSanPhamList = MasterController.LayDanhSachLoaiSanPham();
            model.TongDiemThuong = MasterController.LayTongDiemThuong(User.Identity.Name);
            int trang = !string.IsNullOrEmpty(Request.Params["trang"]) ? Convert.ToInt32(Request.Params["trang"]):1;
            int? idLoaiSanPham = !string.IsNullOrEmpty(Request.Params["idLoaiSanPham"])
                                     ? Convert.ToInt32(Request.Params["idLoaiSanPham"])
                                     : 4;
            var data = SanPhamService.GetListSanPhamInSaleTime(idLoaiSanPham, trang);
            model.SoTrang = data.Item1;
            model.SanPhamList = data.Item2;
            if(!string.IsNullOrEmpty(Request.Params["error"]))
            {
                model.ErrorDangNhap = Request.Params["error"];
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult UploadFile()
        {
            var file = Request.Files[0] as HttpPostedFileBase;
            string fileName = "1" + Path.GetExtension(file.FileName);
            string folderUpload = Request.PhysicalApplicationPath + "Content/";
            string linkAccess = Path.Combine(folderUpload, fileName);
            file.SaveAs(linkAccess);
            return RedirectToAction("Index", "Home");
        }
    }
}
